package com.wushijia.java8.lambda;

import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.averagingInt;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.maxBy;
import static java.util.stream.Collectors.partitioningBy;
import static java.util.stream.Collectors.reducing;
import static java.util.stream.Collectors.summarizingInt;
import static java.util.stream.Collectors.summingInt;

import com.wushijia.model.User;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * description:用流收集数据
 *
 * @author yang
 * @date 2018/5/31 21:23
 */
public class CollectorDemo {


  public static void main(String[] args) {
    List<User> userList = new ArrayList<>();
    userList.add(new User("风清扬", "男", 45));
    userList.add(new User("令狐冲", "男", 24));
    userList.add(new User("岳灵珊", "女", 21));
    userList.add(new User("任盈盈", "女", 22));

    //分组
    Map<String, List<User>> groupBySex1 = userList.stream().collect(groupingBy(User::getSex));
    System.out.println(((User) ((List) groupBySex1.get("女")).get(0)).getName());

    Map<Boolean, List<User>> groupBySex2 = userList.stream().collect(partitioningBy(user -> "女".equals(user.getSex())));
    System.out.println(groupBySex1);
    System.out.println(groupBySex2);

    //统计总数
    long sum1 = userList.stream().collect(Collectors.counting());
    long sum2 = userList.stream().collect(counting());
    long sum3 = userList.stream().count();
    System.out.println(sum1 == userList.size());

    //optional是一个容器，可以包含也可以不包含值
    Comparator<User> userComparator = Comparator.comparing(User::getAge);
    Optional<User> maxByOptional = userList.stream().collect(maxBy(userComparator));
    System.out.println(maxByOptional.isPresent() ? maxByOptional.get().getName() : Optional.empty());
    maxByOptional.ifPresent(t -> {
      System.out.println(t.getName());
    });

    //最大最小值，平均值，总计
    int ageSum = userList.stream().collect(summingInt(User::getAge));
    System.out.println(ageSum);
    System.out.println(userList.stream().max((user1,user2)->user1.getAge()- user2.getAge()).get().getAge());
    System.out.println(userList.stream().min(comparingInt(User::getAge)).get().getAge());

    double avg = userList.stream().collect(averagingInt(User::getAge));
    System.out.println(avg);

    IntSummaryStatistics intSummaryStatistics = userList.stream().collect(summarizingInt(User::getAge));
    System.out.println(intSummaryStatistics.getMin());

    //字符串连接
    System.out.println(userList.stream().map(User::getName).collect(joining("-")));

    int sum = userList.stream().collect(reducing(0, User::getAge, (x, y) -> x + y));
    int sum4 = userList.stream().collect(reducing(0, User::getAge, Integer::sum));
    System.out.println(sum);

    Optional<User> userOptional = userList.stream().collect(reducing((d1, d2) -> d1.getAge() > d2.getAge() ? d1 : d2));
    System.out.println(userOptional.get().getName());

  }
}
